#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFile"
#include <QString>
#include <QDebug>
#include <QMessageBox>
#include <QPainter>

extern LinkList<Node>stu;

mainWindow::mainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainWindow)
{
    ui->setupUi(this);
    setWindowTitle("西北农林科技大学宿舍管理系统");
    static int count=0;
    if(count==0)
    {
        QFile file(QCoreApplication::applicationDirPath()+"/studentdate.txt");
        if(file.exists())
        {
           qDebug()<<"文件存在";
        }
        else{
            qDebug()<<"文件不存在,正在新建文件.";
            file.open( QIODevice::ReadWrite | QIODevice::Text );
        }
        file.close();
        file.open(QIODevice::ReadOnly | QIODevice::Text);


        QTextStream in(&file);
        //遍历信息读入链表
        qDebug()<<"正在读取信息";
        while(!in.atEnd())
        {
            //按行读取信息

            in.setCodec("UTF-8");
            QString str= in.readLine();

            //将字符串分割
            QStringList list = str.split(",");
            QString number = list[0];
            QString name = list[1];
            QString faculty = list [2];
            QString gender = list[3];
            QString roomnu = list[4];
            QString classnu = list[5];

            Node a(number,name,faculty,gender,roomnu,classnu);
            qDebug()<<a.Name<<"已录入";
            stu.Insert(1,a);
        }
        file.close();
        count++;
    }
}

mainWindow::~mainWindow()
{
    delete ui;
}

//保存并退出系统
void mainWindow::on_button5_clicked()
{
    //得出工作路径
    QString path = QCoreApplication::applicationDirPath()+"/studentdate.txt";

    //将文件清空，为重写文件作准备
    QFile file(path);
    file.resize(0);
    file.close();


    //重写文件

    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    for(int i = 0; i< stu.GetLength(); i++){
        Node a;
        stu.GetData(i+1,a);
        out<<a.Number<<",";
        out<<a.Name<<",";
        out<<a.Gender<<",";
        out<<a.Faculty<<",";
        out<<a.RoomNu<<",";
        out<<a.ClassNu<<","<<endl;
    }

    file.close();

    delete this;
}

void mainWindow::on_button1_clicked()//显示学生信息数据库
{
    DBS*stu=new DBS;
    stu->setFixedSize(940,520);
    stu->setWindowTitle("西北农林科技大学宿舍管理系统");
    stu->show();
    delete this;
}

void mainWindow::on_button2_clicked()//添加学生信息
{
    add*ADD=new add;
    ADD->setFixedSize(852,640);
    ADD->setWindowTitle("西北农林科技大学宿舍管理系统");
    ADD->show();

    delete this;
}

void mainWindow::on_button3_clicked()//修改学生信息
{
    modify*a=new modify;
    a->setFixedSize(550,440);
    a->show();
    delete this;
}

void mainWindow::on_button4_clicked()//删除学生信息
{
    Delete*b=new Delete;
    b->setFixedSize(560,400);
    b->setWindowTitle("西北农林科技大学宿舍管理系统");
    b->show();
    delete this;
}
void mainWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);//创建画家对象p
    //绘图操作
    p.drawPixmap(rect(),QPixmap(":/new/prefix1/beffc781ly1gsj5zvodfyj23704siu19.jpg"));

}
